package cn.glutnn.auth.application.controller;

import cn.glutnn.auth.application.entity.Result;
import cn.glutnn.auth.domain.entity.AuthRoleBO;
import cn.glutnn.auth.domain.service.AuthRoleDomainService;
import cn.glutnn.auth.application.convert.AuthRoleConverter;
import cn.glutnn.auth.application.dto.AuthRoleDTO;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Author:   msh
 * Date:     2025/1/13 下午10:52
 * Description:
 */

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Resource
    private AuthRoleDomainService authUserDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController add authRoleDTO:{}", authRoleDTO);
            }
            Preconditions.checkNotNull(authRoleDTO.getRoleName(), "角色名不能为空");
            Preconditions.checkNotNull(authRoleDTO.getRoleKey(), "角色key不能为空");
            AuthRoleBO authRoleBO = AuthRoleConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            return Result.ok(authUserDomainService.add(authRoleBO));
        }catch (Exception e){
            log.error("RoleController add error:{}", e.getMessage(), e);
            return Result.fail("添加失败");
        }
    }

    // 更新角色
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController update authRoleDTO:{}", authRoleDTO);
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            Preconditions.checkNotNull(authRoleDTO.getRoleName(), "角色名不能为空");
            Preconditions.checkNotNull(authRoleDTO.getRoleKey(), "角色key不能为空");
            AuthRoleBO authRoleBO = AuthRoleConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            return Result.ok(authUserDomainService.update(authRoleBO));
        }catch (Exception e){
            log.error("RoleController update error:{}", e.getMessage(), e);
            return Result.fail("更新失败");
        }
    }

    // 删除角色
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController delete authRoleDTO:{}", authRoleDTO);
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            AuthRoleBO authRoleBO = AuthRoleConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            return Result.ok(authUserDomainService.delete(authRoleBO));
        }catch (Exception e){
            log.error("RoleController delete error:{}", e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }
}
